/// 消息模型类
/// 用于存储和处理聊天消息数据
library;
import 'package:chat_project/models/user_model.dart';

enum MessageType {
  text,
  image,
  file,
  audio
}

enum MessageStatus {
  sent,
  delivered,
  read
}

class Message {
  final String id;
  final User sender;
  final String receiverId;
  final String content;
  final MessageType type;
  final MessageStatus status;
  final DateTime createdAt;
  final DateTime? readAt;
  final Map<String, dynamic>? fileInfo;

  Message({
    required this.id,
    required this.sender,
    required this.receiverId,
    required this.content,
    this.type = MessageType.text,
    this.status = MessageStatus.sent,
    DateTime? createdAt,
    this.readAt,
    this.fileInfo,
  }) : createdAt = createdAt ?? DateTime.now();

  // 从JSON构造函数
  factory Message.fromJson(Map<String, dynamic> json) {
    return Message(
      id: json['_id'] ?? json['id'] ?? '',
      sender: json['sender'] is Map 
          ? User.fromJson(json['sender']) 
          : User(id: json['sender'] ?? '', username: '', email: ''),
      receiverId: json['receiver'] ?? '',
      content: json['content'] ?? '',
      type: _messageTypeFromString(json['type']),
      status: _messageStatusFromString(json['status']),
      createdAt: json['createdAt'] != null 
          ? DateTime.parse(json['createdAt']) 
          : DateTime.now(),
      readAt: json['readAt'] != null 
          ? DateTime.parse(json['readAt']) 
          : null,
      fileInfo: json['fileInfo'],
    );
  }

  // 转换为JSON方法
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'sender': sender.id,
      'receiver': receiverId,
      'content': content,
      'type': type.toString().split('.').last,
      'status': status.toString().split('.').last,
      'createdAt': createdAt.toIso8601String(),
      'readAt': readAt?.toIso8601String(),
      'fileInfo': fileInfo,
    };
  }

  // 复制方法，用于更新消息状态
  Message copyWith({
    String? id,
    User? sender,
    String? receiverId,
    String? content,
    MessageType? type,
    MessageStatus? status,
    DateTime? createdAt,
    DateTime? readAt,
    Map<String, dynamic>? fileInfo,
  }) {
    return Message(
      id: id ?? this.id,
      sender: sender ?? this.sender,
      receiverId: receiverId ?? this.receiverId,
      content: content ?? this.content,
      type: type ?? this.type,
      status: status ?? this.status,
      createdAt: createdAt ?? this.createdAt,
      readAt: readAt ?? this.readAt,
      fileInfo: fileInfo ?? this.fileInfo,
    );
  }

  // 辅助方法将字符串转换为MessageType枚举
  static MessageType _messageTypeFromString(String? typeStr) {
    switch (typeStr) {
      case 'image':
        return MessageType.image;
      case 'file':
        return MessageType.file;
      case 'audio':
        return MessageType.audio;
      case 'text':
      default:
        return MessageType.text;
    }
  }

  // 辅助方法将字符串转换为MessageStatus枚举
  static MessageStatus _messageStatusFromString(String? statusStr) {
    switch (statusStr) {
      case 'delivered':
        return MessageStatus.delivered;
      case 'read':
        return MessageStatus.read;
      case 'sent':
      default:
        return MessageStatus.sent;
    }
  }
} 